package com.example.shoppings.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.shoppings.R;
import com.example.shoppings.activity.PayActivity;
import com.example.shoppings.sql.Orders;
import com.example.shoppings.sql.Products;
import com.example.shoppings.utils.BaseUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by hasee on 2017-05-12.
 */

public class AddCartFragment extends Activity {
    @BindView(R.id.tvsku1)
    TextView tvsku1;
    @BindView(R.id.tvsku2)
    TextView tvsku2;
    @BindView(R.id.iv_reduce)
    ImageView ivReduce;
    @BindView(R.id.tvNum)
    TextView tvNum;
    @BindView(R.id.iv_add)
    ImageView ivAdd;
    @BindView(R.id.tv_kucun)
    TextView tvKucun;
    @BindView(R.id.tv_allPrice)
    TextView tvAllPrice;
    @BindView(R.id.tv_singlePrice)
    TextView tvSinglePrice;
    @BindView(R.id.commitOrder)
    TextView tvCommitOrder;
    private View view;

    private String []sku;
    private String selectSku;
    private String singlePrice,allPrice;
    private int allNum,buyNum=1;
    private Products products;



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_addcart);
        WindowManager windowManager = getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = (int)(display.getWidth()); //设置宽度display.getWidth()可以设置成数字型参数，这里表示的是当界面有多宽，弹出框的宽度就有多宽
        getWindow().setAttributes(lp);//执行
        ButterKnife.bind(this);
        products= (Products) getIntent().getSerializableExtra("product");
        setData();
    }

    private void setData(){
        sku=products.getProSKU().split(",");
        tvsku1.setText(sku[0]);
        tvsku2.setText(sku[1]);

        tvSinglePrice.setText("单价："+products.getProPrice());
        tvKucun.setText("库存: "+products.getProNum());
        tvNum.setText(buyNum+"");
        tvAllPrice.setText("总价："+products.getProPrice());
    }

    private void setChangeData(){

        tvNum.setText(buyNum+"");
        tvAllPrice.setText("总价: "+(Integer.parseInt(products.getProPrice())*buyNum)+"");
    }


    private void setBaseSku(){
        tvsku1.setBackgroundResource(R.drawable.shap_corner_white);
        tvsku2.setBackgroundResource(R.drawable.shap_corner_white);
        tvsku1.setTextColor(getResources().getColor(R.color.lightblack));
        tvsku2.setTextColor(getResources().getColor(R.color.lightblack));
    }

    private void commitOrder(){
        Orders orders=new Orders();
        orders.setOrderPrice(allPrice+"");
        long orderId=System.currentTimeMillis();
        products.setProSKU(selectSku);
        orders.setOrderId(orderId);
        orders.setCreateTime(BaseUtils.formatData());
        orders.setProducts(products);
        orders.save();
        Intent intent=new Intent(this, PayActivity.class
        );
        intent.putExtra("orderId",orderId);
        startActivity(intent);
    }


    @OnClick({R.id.tvsku1, R.id.tvsku2, R.id.iv_reduce, R.id.iv_add,R.id.commitOrder})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvsku1:
                setBaseSku();
                selectSku=tvsku1.getText().toString();
                tvsku1.setTextColor(getResources().getColor(R.color.white));
                tvsku1.setBackgroundResource(R.color.baseblue);
                break;
            case R.id.tvsku2:
                setBaseSku();
                selectSku=tvsku2.getText().toString();
                tvsku2.setTextColor(getResources().getColor(R.color.white));
                tvsku2.setBackgroundResource(R.color.baseblue);
                break;
            case R.id.iv_reduce:
                if(buyNum>1){
                    buyNum--;
                    setChangeData();
                }
                break;
            case R.id.iv_add:
                if(buyNum<Integer.parseInt(products.getProNum())){
                    buyNum++;
                    setChangeData();
                }
                break;
            case R.id.commitOrder:
                commitOrder();
                break;
        }
    }
}
